package com.scada.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scada.dao.IRemindConfigDao;
import com.scada.dao.IRemindDao;
import com.scada.inf.RemindVisitor;
import com.scada.model.bean.MemberBuyService;
import com.scada.model.bean.MemberInfo;
import com.scada.model.bean.MemberPlanBuy;
import com.scada.model.bean.MemberReturnVisit;
import com.scada.model.bean.RemindConfig;
import com.scada.model.result.ResponseBean;
import com.scada.model.search.MakeOverExpireSearch;
import com.scada.model.search.MemberRetrunSearch;
import com.scada.model.search.MemberReturnExpireSearch;
import com.scada.model.search.PlanBuyExpireSearch;
import com.scada.model.search.PlanBuyReturnSearch;
import com.scada.model.search.ServiceExpireSearch;
import com.scada.util.DateUtil;

@Service("remindVisitorService")
@WebService
@SOAPBinding(style = Style.RPC)
public class RemindVisitorImpl implements RemindVisitor{

	@Autowired
	private IRemindDao remindDao;
	@Autowired
	private IRemindConfigDao remindConfigDao;
	
	@Override
	public ResponseBean getServiceExpirePageData(ServiceExpireSearch serviceExpireSearch) {
		
		ResponseBean responseBean = new ResponseBean();
		Map<String,Object> map = new HashMap<String,Object>();
		
		if(serviceExpireSearch.getBeforeExpireNum() == null) {
			try {
				RemindConfig rc = remindConfigDao.getRemindConfigByCode("service_expire_before");
				if(rc != null) {
					serviceExpireSearch.setBeforeExpireNum(rc.getConfigNum());
				}else {
					serviceExpireSearch.setBeforeExpireNum(30);
				}
			}catch (Exception e) {
				serviceExpireSearch.setBeforeExpireNum(30);
			}
        	
        }
		
        if(serviceExpireSearch.getAfterExpireNum() == null) {
        	try {
				RemindConfig rc = remindConfigDao.getRemindConfigByCode("service_expire_after");
				if(rc != null) {
					serviceExpireSearch.setAfterExpireNum(rc.getConfigNum()*-1);
				}else {
					serviceExpireSearch.setAfterExpireNum(-90);
				}
			}catch (Exception e) {
				serviceExpireSearch.setAfterExpireNum(-90);
			}
        	
        }else {
        	if(serviceExpireSearch.getAfterExpireNum() > 0) {
        		serviceExpireSearch.setAfterExpireNum(serviceExpireSearch.getAfterExpireNum()*-1);
        	}
        }
		
		try{
			map.put("isShow", MemberBuyService.IsShowCode.YES.getKey());
			map.put("ifRenew", MemberBuyService.ifRenewCode.YES.getKey());
			map.put("ifExpire", MemberBuyService.ifExpireCode.NO.getKey());
			map.put("serviceId", serviceExpireSearch.getServiceId());
			map.put("memberId", serviceExpireSearch.getMemberId());
			map.put("serviceType", serviceExpireSearch.getServiceType());
			map.put("beforeExpireNum", serviceExpireSearch.getBeforeExpireNum());
			map.put("afterExpireNum", serviceExpireSearch.getAfterExpireNum());
			responseBean.addObject("resultCount", remindDao.getServiceExpirePageCount(map));
			map.put("start", serviceExpireSearch.getStart());
			map.put("pageSize", serviceExpireSearch.getLength());
            responseBean.addObject("resultList", remindDao.getServiceExpirePageList(map));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean getPlanBuyExpirePageData(PlanBuyExpireSearch planBuyExpireSearch) {
		
		ResponseBean responseBean = new ResponseBean();
		Map<String,Object> map = new HashMap<String,Object>();
		
		if(planBuyExpireSearch.getBeforeExpireNum() == null) {
			try {
				RemindConfig rc = remindConfigDao.getRemindConfigByCode("plan_buy_expire_before");
				if(rc != null) {
					planBuyExpireSearch.setBeforeExpireNum(rc.getConfigNum());
				}else {
					planBuyExpireSearch.setBeforeExpireNum(15);
				}
			}catch (Exception e) {
				planBuyExpireSearch.setBeforeExpireNum(15);
			}
        	
        }
		
        if(planBuyExpireSearch.getAfterExpireNum() == null) {
        	try {
				RemindConfig rc = remindConfigDao.getRemindConfigByCode("plan_buy_expire_after");
				if(rc != null) {
					planBuyExpireSearch.setAfterExpireNum(rc.getConfigNum()*-1);
				}else {
					planBuyExpireSearch.setAfterExpireNum(-15);
				}
			}catch (Exception e) {
				planBuyExpireSearch.setAfterExpireNum(-15);
			}
        	
        }else {
        	if(planBuyExpireSearch.getAfterExpireNum() > 0) {
        		planBuyExpireSearch.setAfterExpireNum(planBuyExpireSearch.getAfterExpireNum()*-1);
        	}
        }
        
		try{
			map.put("isShow", MemberPlanBuy.IsShowCode.YES.getKey());
			map.put("serviceId", planBuyExpireSearch.getServiceId());
			map.put("memberId", planBuyExpireSearch.getMemberId());
			map.put("serviceType", planBuyExpireSearch.getServiceType());
			map.put("beforeExpireNum", planBuyExpireSearch.getBeforeExpireNum());
			map.put("afterExpireNum", planBuyExpireSearch.getAfterExpireNum());
			responseBean.addObject("resultCount", remindDao.getPlanBuyExpirePageCount(map));
			map.put("start", planBuyExpireSearch.getStart());
			map.put("pageSize", planBuyExpireSearch.getLength());
            responseBean.addObject("resultList", remindDao.getPlanBuyExpirePageList(map));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean getPlanBuyReturnPageData(PlanBuyReturnSearch planBuyReturnSearch) {
		
		ResponseBean responseBean = new ResponseBean();
		Map<String,Object> map = new HashMap<String,Object>();
		
		int visitNum = 15;
		
		try {
			RemindConfig rc = remindConfigDao.getRemindConfigByCode("return_day_interval");
			if(rc != null) {
				visitNum = rc.getConfigNum();
			}
		}catch (Exception e) {
			
		}
		
		try{
			map.put("isShow", MemberPlanBuy.IsShowCode.YES.getKey());
			map.put("ifFifteenRevisit", MemberPlanBuy.ifFifteenRevisitCode.YES.getKey());
			map.put("serviceId", planBuyReturnSearch.getServiceId());
			map.put("memberId", planBuyReturnSearch.getMemberId());
			map.put("serviceType", planBuyReturnSearch.getServiceType());
			map.put("nowDate", DateUtil.format(new Date(), "yyyy-MM-dd"));
			map.put("visitNum", visitNum);
			responseBean.addObject("resultCount", remindDao.getPlanBuyReturnPageCount(map));
			map.put("start", planBuyReturnSearch.getStart());
			map.put("pageSize", planBuyReturnSearch.getLength());
            responseBean.addObject("resultList", remindDao.getPlanBuyReturnPageList(map));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean getMRExpirePageData(MemberReturnExpireSearch mrExpireSearch) {
		
		ResponseBean responseBean = new ResponseBean();
		Map<String,Object> map = new HashMap<String,Object>();
		
		if(mrExpireSearch.getBeforeExpireNum() == null) {
			try {
				RemindConfig rc = remindConfigDao.getRemindConfigByCode("mr_expire_before");
				if(rc != null) {
					mrExpireSearch.setBeforeExpireNum(rc.getConfigNum());
				}else {
					mrExpireSearch.setBeforeExpireNum(15);
				}
			}catch (Exception e) {
				mrExpireSearch.setBeforeExpireNum(15);
			}
        	
        }
		
        if(mrExpireSearch.getAfterExpireNum() == null) {
        	try {
				RemindConfig rc = remindConfigDao.getRemindConfigByCode("mr_expire_after");
				if(rc != null) {
					mrExpireSearch.setAfterExpireNum(rc.getConfigNum()*-1);
				}else {
					mrExpireSearch.setAfterExpireNum(-15);
				}
			}catch (Exception e) {
				mrExpireSearch.setAfterExpireNum(-15);
			}
        	
        }else {
        	if(mrExpireSearch.getAfterExpireNum() > 0) {
        		mrExpireSearch.setAfterExpireNum(mrExpireSearch.getAfterExpireNum()*-1);
        	}
        }
		
		try{
			map.put("isShow", MemberReturnVisit.IsShowCode.YES.getKey());
			map.put("memberId", mrExpireSearch.getMemberId());
			map.put("beforeExpireNum", mrExpireSearch.getBeforeExpireNum());
			map.put("afterExpireNum", mrExpireSearch.getAfterExpireNum());
			responseBean.addObject("resultCount", remindDao.getMRExpirePageCount(map));
			map.put("start", mrExpireSearch.getStart());
			map.put("pageSize", mrExpireSearch.getLength());
            responseBean.addObject("resultList", remindDao.getMRExpirePageList(map));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean getMemberReturnPageData(MemberRetrunSearch memberRetrunSearch) {
		
		ResponseBean responseBean = new ResponseBean();
		Map<String,Object> map = new HashMap<String,Object>();
		
		int visitNum = 15;
		String custTypeStr = "";
		try {
			RemindConfig rc = remindConfigDao.getRemindConfigByCode("return_day_interval");
			if(rc != null) {
				visitNum = rc.getConfigNum();
			}
			
			if(StringUtils.isBlank(memberRetrunSearch.getCustTypeStr()) || StringUtils.equals("null", memberRetrunSearch.getCustTypeStr())) {
				RemindConfig custTypeRc = remindConfigDao.getRemindConfigByCode("member_return_cust_type");
				if(rc != null) {
					custTypeStr = custTypeRc.getConfigStr();
				}
			}else {
				custTypeStr = memberRetrunSearch.getCustTypeStr();
			}
		}catch (Exception e) {
			
		}
		
		try{
			map.put("custTypeStr", custTypeStr);
			map.put("isShow", MemberPlanBuy.IsShowCode.YES.getKey());
			map.put("ifFifteenRevisit", MemberInfo.ifFifteenRevisitCode.YES.getKey());
			map.put("visitNum", visitNum);
			map.put("nowDate", DateUtil.format(new Date(), "yyyy-MM-dd"));
			map.put("memberId", memberRetrunSearch.getMemberId());
			responseBean.addObject("resultCount", remindDao.getMemberReturnPageCount(map));
			map.put("start", memberRetrunSearch.getStart());
			map.put("pageSize", memberRetrunSearch.getLength());
            responseBean.addObject("resultList", remindDao.getMemberReturnPageList(map));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean MakeOverExpirePageData(MakeOverExpireSearch makeOverExpireSearch) {
		
		ResponseBean responseBean = new ResponseBean();
		Map<String,Object> map = new HashMap<String,Object>();
		
		
		if(makeOverExpireSearch.getCycle() == null) {
			try {
				RemindConfig rc = remindConfigDao.getRemindConfigByCode("make_over_cycle");
				if(rc != null) {
					makeOverExpireSearch.setCycle(rc.getConfigNum());
				}else {
					makeOverExpireSearch.setCycle(2);
				}
			}catch (Exception e) {
				makeOverExpireSearch.setCycle(2);
			}
		}
		
		
		try{
			map.put("isShow", MemberReturnVisit.IsShowCode.YES.getKey());
			map.put("memberId", makeOverExpireSearch.getMemberId());
			map.put("beforeExpireNum", (makeOverExpireSearch.getCycle()*7));
			map.put("afterExpireNum", 0);
			map.put("nowDate", DateUtil.format(new Date(), "yyyy-MM-dd"));
			responseBean.addObject("resultCount", remindDao.getMakeOverExpirePageCount(map));
			map.put("start", makeOverExpireSearch.getStart());
			map.put("pageSize", makeOverExpireSearch.getLength());
            responseBean.addObject("resultList", remindDao.getMakeOverExpirePageList(map));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

}
